package com.mahees.portfolio.controller

import com.mahees.portfolio.domain.DBFile;

import grails.test.*

class RetrieveFileControllerTests extends ControllerUnitTestCase {
	
	byte[] savedFile
	String fileName = 'apple'
	
    protected void setUp() {
        super.setUp()
		
		controller.request.format = 'image';
		
		savedFile = "yaay".bytes;
		
		mockDomain(DBFile)

		DBFile savedDBFile = new DBFile(name : fileName, size : 10, contentType : 'image');
		savedDBFile.encodeBase64(savedFile);
		savedDBFile.save()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testShow_FetchExistingFile_Success() {
		controller.params.id = fileName
		
		controller.show();

		assert DBFile.count() == 1
		//TODO: inspect how to get here
		assert controller.response.outputStream.targetStream.toByteArray() == savedFile
    }
	
	void testShow_FetchNonExistingFile_GetRedirected() {
		controller.params.id = "fileName"
		
		controller.show();

		assert DBFile.count() == 1
		assert controller.redirectArgs.absolute
		assert controller.redirectArgs.url == 'hello'
	}
}
